$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var tianjian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["tianjian"];
    var feitianjian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["feitianjian"];
    var fujian = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["fujian"];
    var guangxi = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangxi"];
    var xinsanli = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["xinsanli"];
    var guangzhoumatou = JSON.parse(localStorage.getItem("arr_role"))["warehouse"]["guangzhoumatou"];

    var pass = 1;

    // 默认不显示
    $(".con li").hide();
    if (feitianjian) {
        $(".con li.feitianjin").show();
    }
    if (tianjian) {
        $(".con li.tianjin").show();
    }
    if (fujian) {
        $(".con li.fujian").show();
    }
    if (guangxi) {
        $(".con li.guangxi").show();
    }
    if (xinsanli) {
        $(".con li.xinsanli").show();
    }
    if (guangzhoumatou) {
        $(".con li.guangzhoumatou").show();
    }
    if (!feitianjian && !tianjian && !fujian && !guangxi && !xinsanli && !guangzhoumatou) {
        // 默认兼容旧版本行为：没有选非天津的按非天津处理
        $(".con li.feitianjin").show();
    }

    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "POST",
        url: url + "/app/productinorder/getWareHouseForProductIn",
        data: {},
        success: function (res) {
            pass = 1;
            loading_hide();
            if (res.success) {
                // var data = res.result;
                // var dataHtml = '<option value="">请选择入库仓库</option>';
                // for (var i = 0; i < data.length; i++) {
                //     dataHtml += '<option value="' + (data[i].warehouseName + ',' + data[i].warehouseCode) + '">' + data[i].warehouseName + '</option>';
                // }
                // $("div.div_piWh select.warehouse").html(dataHtml);
                var dataBm = res.department;
                var dataHtmlBm = '<option value="">请选择入库部门</option>';
                for (var i = 0; i < dataBm.length; i++) {
                    dataHtmlBm += '<option value="' + (dataBm[i].departmentName + ',' + dataBm[i].departmentCode) + '">' + dataBm[i].departmentName + '</option>';
                }
                $("div.div_piWh select.department").html(dataHtmlBm);
            } else {
                console.log(res);
                $alert(res.message);
            }
        },
        error: function (error) {
            pass = 1;
            loading_hide();
            console.log(error);
            $alert(error.responseText);
        }
    });
    //点击 成品入库
    $("li.productIn").on("click", function () {
        $(".mengceng").show();
        $("div.div_piWh").show();
        $("div.div_piWh select").val("").trigger("change");
    });
    //点击 切换公司 取消
    $("div.div_piWh .piWh_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_piWh").hide();
    });
    //点击 切换公司 确认
    $("div.div_piWh .piWh_queding").on("click", function () {
        // var warehouse = $("div.div_piWh select.warehouse").val();
        var department = $("div.div_piWh select.department").val();
        // if (!warehouse) {
        //     return $alert("请选择入库的仓库");
        // } else
        if (!department) {
            return $alert("请选择入库的部门");
        } else {
            // localStorage.setItem("productInWh", warehouse)
            localStorage.setItem("productInDp", department)
            window.location.href = "product_one.html";
        }
    });

});
